package name.koppernickus.lsp.analyzer;

import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Set;

import static com.google.common.base.Strings.repeat;
import static name.koppernickus.lsp.analyzer.ClasspathBuilder.buildClasspathUsingJARFileNames;

public class Stats {

    @Test
    public void shouldPrintAllInterestingSubclassingsFromJDK() throws Exception {
        Set<Subclassing> subclassings = printStatsAbout("rt.jar");
        printSubclassingsFromPackage(subclassings, "java.lang");
        printSubclassingsFromPackage(subclassings, "java.util");
    }

    @Test
    public void shouldPrintAllInterestingSubclassingsFromJUnit() throws Exception {
        Set<Subclassing> subclassings = printStatsAbout("junit-4.8.2.jar");
        printSubclassingsFromPackage(subclassings, "org.junit");
    }

    @Test
    public void shouldPrintAllInterestingSubclassingsFromGuava() throws Exception {
        Set<Subclassing> subclassings = printStatsAbout("guava-r07.jar");
        printSubclassingsFromPackage(subclassings, "com.google");
    }

    @Test
    public void shouldPrintAllInterestingSubclassingsFromCamelCore() throws Exception {
        Set<Subclassing> subclassings = printStatsAbout("camel-core-2.4.0.jar");
        printSubclassingsFromPackage(subclassings, "org.apache");
    }

    @Test
    public void shouldPrintAllInterestingSubclassingsFromActiveMQCore() throws Exception {
        Set<Subclassing> subclassings = printStatsAbout("activemq-core-5.4.0.jar");
        printSubclassingsFromPackage(subclassings, "org.apache");
    }

    private Set<Subclassing> printStatsAbout(String... jarFiles) throws IOException, ClassNotFoundException {
        String classpath = buildClasspathUsingJARFileNames(jarFiles);
        Analyzer analyzer = new Analyzer(classpath);
        Set<Subclassing> subclassings = analyzer.findAllInterestingSubclassings();
        System.out.println(repeat("=", 120));
        System.out.println("Subclassing stats about " + Arrays.toString(jarFiles));
        System.out.println(repeat("=", 120));
        analyzer.printStats();
        return subclassings;
    }

    private void printSubclassingsFromPackage(Set<Subclassing> subclassings, String packageName) {
        System.out.println(repeat("=", 120));
        System.out.println("Interesting subclassings from " + packageName);
        System.out.println(repeat("=", 120));
        for (Subclassing subclassing : subclassings) {
            if (subclassing.getSubClass().getName().startsWith(packageName + ".")) {
                System.out.println(subclassing);
            }
        }
    }

}
